package com.pay.bean;

import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * 支付宝异步支付结果对象。
 * 详情见：https://docs.open.alipay.com/62/103566
 * Created by ThreeManTeam on 2016/11/20.
 */

@Getter
@Setter
public class AliPayResultAsync extends AliPayResult implements IPayResult, Serializable {

    public AliPayResultAsync() {
        setReqSuccess(true);
    }

    /**
     * 交易创建时间，格式为yyyy-MM-dd HH:mm:ss， 可空。
     */
    private Date gmtCreate;

    /**
     * 交易付款时间，格式为yyyy-MM-dd HH:mm:ss， 可空。
     */
    private Date gmtPayment;

    /**
     * 交易关闭时间，格式为yyyy-MM-dd HH:mm:ss， 可空。
     */
    private Date gmtClose;

    /**
     * 退款状态
     */
    private String refundStatus;

    /**
     * 退款时间
     */
    private Date gmtRefund;

    public void initialEx(Map<String, String> params, String partnerId, String aliPublicKey) {
        super.initialEx(params, partnerId, aliPublicKey);
        _initial(params);
    }

    @Override
    public Map<String, String> initial(Map<String, String[]> notifyParams, String partnerId, String aliPublicKey) {
        Map<String, String> params = super.initial(notifyParams, partnerId, aliPublicKey);
        _initial(params);
        return params;
    }

    private void _initial(Map<String, String> params) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            if (isReqSuccess() && isVerifySuccess()) {
                if (params.get("gmt_create") != null && params.get("gmt_create").length() > 0) {
                    setGmtCreate(sdf.parse(params.get("gmt_create")));
                }

                if (params.get("gmt_payment") != null && params.get("gmt_payment").length() > 0) {
                    setGmtPayment(sdf.parse(params.get("gmt_payment")));
                }

                if (params.get("gmt_close") != null && params.get("gmt_close").length() > 0) {
                    setGmtClose(sdf.parse(params.get("gmt_close")));
                }

                if (params.get("refund_status") != null && params.get("refund_status").length() > 0) {
                    setRefundStatus(params.get("refund_status"));
                }

                if (params.get("gmt_refund") != null && params.get("gmt_refund").length() > 0) {
                    setGmtRefund(sdf.parse(params.get("gmt_refund")));
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public String getResponse() {
        if (isReqSuccess() && isVerifySuccess())
            return "success";
        return "fail";
    }

    @Override
    public String getPayMode() {
        return PayMode.ALI_PAY.name();
    }

    @Override
    public BigDecimal getPaidAmount() {
        return getTotalFee();
    }

    @Override
    public Date getPayTime() {
        return getGmtPayment();
    }

    @Override
    public boolean isPaid() {
        return "TRADE_SUCCESS".equals(getTradeStatus()) || "TRADE_FINISHED".equals(getTradeStatus());
    }

    @Override
    public boolean isQueryResult() {
        return false;
    }

    @Override
    public TradeState getStandardTradeStatus() {
        TradeState state = null;
        if (getTradeStatus() != null) {
            switch (getTradeStatus()) {
                case "WAIT_BUYER_PAY":
                    state = TradeState.PAYING;
                    break;
                case "TRADE_CLOSED":
                    state = TradeState.TRADE_FAIL;
                    break;
                case "TRADE_SUCCESS":
                    state = TradeState.TRADE_SUCCESS;
                    break;
                case "TRADE_PENDING":
                    state = TradeState.PAYING;
                    break;
                case "TRADE_FINISHED":
                    state = TradeState.TRADE_SUCCESS;
                    break;
            }
        }
        return state;
    }
}
